﻿using BLL.Base;
using CommandService;
using Entity;
using Entity.DbEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.product
{
    public class ProductBLL : PubMethod
    {
        Commands cmd;
        LoginInfo userLogin;
        Tools.TxtLogHelper txt;
        ReturnResult rsl;
        SystemBLL sysBLL;
        List<OperateModel> operList;
        OperateModel oper;
        public ProductBLL()
        {
            cmd = new Commands();
            userLogin = new LoginInfo();
        }
       /// <summary>
       /// 根据母件条码获取有效的组装记录
       /// </summary>
       /// <param name="barCode"></param>
       /// <returns></returns>
        public ReturnResult AssembleList(string barCode)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from V_ProductAssemble_record ");
                sb.Append("where momBarCode='"+ barCode +"' and ");
                sb.Append("status='Y'");
                var table = cmd.GetDataTable(sb.ToString());
                if (table.Rows.Count >0)
                {
                    rsl.Data = cmd.TableToDictionary(table);
                    rsl.Result = true;
                }
             
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 拆解操作
        /// </summary>
        /// <param name="barCode">母件条码编码</param>
        /// <returns></returns>
        public ReturnResult BreakAssemble(string barCode)
        {
            rsl = new ReturnResult();
            try
            {
                operList = new List<OperateModel>();
                StringBuilder sb = new StringBuilder();
                sb.Append("select childBarCode,barType from V_ProductAssemble_record ");
                sb.Append("where momBarCode='" + barCode + "' and ");
                sb.Append("status='Y'");
                var table = cmd.GetDataTable(sb.ToString());
                sb.Clear();
                sb.Append("select code from sys_dictionaries_info ");
                sb.Append("where types='pici'");//获取所有批次编码
                var dicTable = cmd.GetDataTable(sb.ToString());
                #region 组装记录状态改为无效
                DbAssembleRecord record = new DbAssembleRecord();
                record.momBarCode = barCode;
                record.status = "N";
                oper = new OperateModel();
                oper.Data = record;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "momBarCode";
                operList.Add(oper);
                #endregion
                #region 母件SN码状态改为无效
                DbProductBar bar = new DbProductBar();
                bar.barCode= barCode;
                bar.barStatus = "bar-drop";
                oper = new OperateModel();
                oper.Data = bar;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "barCode";
                operList.Add(oper);
                #endregion
                #region 子件条码改为待入库状态
                foreach (DataRow r in table.Rows)
                {
                    var dr = dicTable.Select("code='"+ r["barType"].ToString() +"'");
                    if (dr.Length < 1)
                    {//如果子件为批次码，则不需更新条码状态
                        bar = new DbProductBar();
                        bar.barCode = r["childBarCode"].ToString();
                        bar.barStatus = "need-in";
                        oper = new OperateModel();
                        oper.Data = bar;
                        oper.Types = OperateModel.OperateType.Edit;
                        oper.Conditions = "barCode";
                        operList.Add(oper);
                    }
             
                }
                #endregion
                #region 添加拆卸记录
                DbProductionsRecord pr = new DbProductionsRecord();
                pr.barCode = barCode;
                pr.createtime = getNow();
                pr.createuser = userLogin.UserInfo().userCode;
                pr.doType = "broken";
                pr.isDel =0;
                pr.status = "Y";
                pr.memo = "拆解";
                oper = new OperateModel();
                oper.Data = pr;
                oper.Types = OperateModel.OperateType.Add;
                operList.Add(oper);
                #endregion
                if (operList.Count > 0)
                {
                    rsl = cmd.MoreUpdate(operList);
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
    }
}
